# _*_ coding:utf-8 _*_
# @Time  : 2022.10.24
# @Author: zizlee
import json
import pathlib
import random
import time

import requests
import datetime
import pandas as pd
from urllib3 import disable_warnings
disable_warnings()

HOST = 'https://210.13.218.130:9000/v1/'

USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " \
             "Chrome/86.0.4240.198 Safari/537.36"

UNITS = {
    'CU': '吨',
    'BC': '吨',
    'AL': '吨',
    'ZN': '吨',
    'PB': '吨',
    'NI': '吨',
    'SN': '吨',
    'RB': '吨',
    'WR': '吨',
    'HC': '吨',
    'RU': '吨',
    'NR': '吨',
    'LU': '吨',
    'FU': '吨',
    'BU': '吨',
    'SP': '吨',
    'SS': '吨',
    'AU': '克',
    'AG': '千克',
    'SC': '桶',
    'AO': '吨'
}


# 分离英文和数字
def extra_variety_en(ustr):
    return ''.join(list(filter(lambda s: s.isalpha(), ustr))).upper()


# 获取单位
def get_volume_unit(en):
    unit = UNITS.get(en)
    if not unit:
        raise ValueError('{}的单位不存在!'.format(en))
    return unit


# 获取文件
def get_delivery_file(month):
    headers = {'User-Agent': USER_AGENT,
               'Host': 'www.shfe.com.cn'}
    r = requests.get('http://www.shfe.com.cn/data/instrument/ExchangeDelivery{}.dat'.format(month),
                     headers=headers)
    with open('shfe/delivery/{}.json'.format(month), 'w') as fp:
        json.dump(r.json(), fp, indent=2, ensure_ascii=False)
    print('获取{}.SHF交割数据文件完成！'.format(month))


# 解析文件
def parse_delivery_file(month, last_date):
    with open('shfe/delivery/{}.json'.format(month), 'r') as fp:
        d_data = json.load(fp)['ExchangeDelivery']

    df = pd.DataFrame(d_data)
    df['DELIVERYAMOUNT'] = df['DELIVERYAMOUNT'].astype(float)
    df['DELIVERYVOLUME'] = df['DELIVERYVOLUME'].astype(float)
    df = df.groupby(by='INSTRUMENTID', as_index=False)[['DELIVERYAMOUNT', 'DELIVERYVOLUME']].sum().round(2)
    df['variety_en'] = df['INSTRUMENTID'].apply(extra_variety_en)
    df['volume_unit'] = df['variety_en'].apply(get_volume_unit)
    df['amount_unit'] = '元'
    df['delivery_date'] = last_date
    df.rename(columns={'DELIVERYAMOUNT': 'amount', 'DELIVERYVOLUME': 'volume'}, inplace=True)
    del df['INSTRUMENTID']
    df_data = df.to_dict(orient='records')
    print('----- 解析{}.SHF交割文件成功,数量:{} -----'.format(last_date, len(df_data)))
    if len(df_data) > 0:
        folder = pathlib.Path("resolution/{}/".format(last_date))
        if not folder.exists():
            folder.mkdir(parents=True)
        resolution_delivery_file = pathlib.Path("resolution/{}/SHF_Delivery.json".format(last_date))
        # 保存到本地文件
        with open(resolution_delivery_file, 'w', encoding='utf8') as fp:
            json.dump(df_data, fp, indent=2, ensure_ascii=False)


def save_delivery_to_server(delivery_date: datetime.date):
    quote_date_string = delivery_date.strftime('%Y%m%d')
    resolution_delivery_file = pathlib.Path("resolution/{}/SHF_Delivery.json".format(quote_date_string))
    if not resolution_delivery_file.exists():
        raise ValueError('{}.SHF交割数据文件不存在,请先解析保存！'.format(quote_date_string))
    # 读取本地文件
    with open(resolution_delivery_file, 'r', encoding='utf8') as fp:
        json_data = json.load(fp)
    # 保存到目标API
    r = requests.post(url=HOST + 'exchange/delivery/', json=json_data, verify=False)
    print(r.json())


if __name__ == '__main__':
    start = datetime.date(2023, 11, 1)  # 201310开始
    end = datetime.date(2023, 12, 1)
    business_days_rng = pd.date_range(start, end, freq='BM')
    not_delivery_dates = []
    for d in business_days_rng:
        ym = d.strftime('%Y%m')
        month_last = d.strftime('%Y%m%d')
        print(ym, month_last)
        get_delivery_file(ym)
        parse_delivery_file(ym, last_date=month_last)
        save_delivery_to_server(d)
        time.sleep(1)
